home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 10868 / 10868.xpi / chrome / sync.jar / content / fennec-tabs.js < prev    next >
Text File  |  2010-02-02  |  5KB  |  150 lines

  1. // Make sure this is the only instance of the page
  2. Components.utils.import("resource://weave/service.js");
  3. Weave.Utils.ensureOneOpen(window);
  4.  
  5. const Cc = Components.classes;
  6. const Ci = Components.interfaces;
  7.  
  8. let RemoteTabViewer = {
  9.   show: function RemoteTabViewer_show() {
  10.     // Don't do anything if the tabs engine isn't ready
  11.     if (!Weave.Engines.get("tabs"))
  12.       return;
  13.  
  14.     this._maybeNotify();
  15.     this._populateTabs();
  16.     this._refetchTabs();
  17.   },
  18.  
  19.   _maybeNotify: function _maybeNotify() {
  20.     // Don't notify if the tab engine has new tabs or the user dismissed it
  21.     let prefs = Weave.Svc.Prefs;
  22.     if (prefs.get("notifyTabState") == 0)
  23.       return;
  24.  
  25.     let chromeWin = window.QueryInterface(Ci.nsIInterfaceRequestor).
  26.       getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShellTreeItem).
  27.       rootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor).
  28.       getInterface(Ci.nsIDOMWindow).QueryInterface(Ci.nsIDOMChromeWindow);
  29.  
  30.     // No need to reshow the notification if it's still open
  31.     let notifyBox = chromeWin.getNotificationBox(window);
  32.     if (notifyBox.getNotificationWithValue("remote-tabs") != null)
  33.       return;
  34.  
  35.     let message = Weave.Str.sync.get("remote.notification.label");
  36.     let notification = notifyBox.appendNotification(message, "remote-tabs", "",
  37.       notifyBox.PRIORITY_INFO_LOW);
  38.  
  39.     // Wrap the close function to find out if the user clicks the X
  40.     let close = notification.close;
  41.     notification.close = function() {
  42.       // Once the user dismisses the dialog, remember that and don't show again
  43.       prefs.set("notifyTabState", 0);
  44.       close.apply(notification, arguments);
  45.     };
  46.   },
  47.  
  48.   _refetchTabs: function _refetchTabs() {
  49.     // Don't bother refetching tabs if we already did so recently
  50.     let lastFetch = Weave.Svc.Prefs.get("lastTabFetch", 0);
  51.     let now = Math.floor(Date.now() / 1000);
  52.     if (now - lastFetch < 30)
  53.       return;
  54.  
  55.     // Asynchronously fetch the tabs
  56.     setTimeout(function() {
  57.       let engine = Weave.Engines.get("tabs");
  58.       let lastSync = engine.lastSync;
  59.  
  60.       // Force a sync only for the tabs engine
  61.       engine.lastModified = null;
  62.       engine.sync();
  63.       Weave.Svc.Prefs.set("lastTabFetch", now);
  64.  
  65.       // Only reload the page if something synced
  66.       if (engine.lastSync != lastSync)
  67.         location.reload();
  68.     }, 0);
  69.   },
  70.  
  71.   _populateTabs: function _populateTabs() {
  72.     // Clear out all child elements from holder first, so we don't
  73.     // end up adding duplicate rows.
  74.     let engine = Weave.Engines.get("tabs");
  75.     let holder = document.getElementById("tabList");
  76.     if (holder.hasChildNodes()) {
  77.       while (holder.childNodes.length >= 1)
  78.         holder.removeChild(holder.firstChild);
  79.     }
  80.  
  81.     // Generate the list of tabs
  82.     let haveTabs = false;
  83.     for (let [guid, client] in Iterator(engine.getAllClients())) {
  84.       haveTabs = true;
  85.  
  86.       // Create the client node, but don't add it in-case we don't show any tabs
  87.       let appendClient = true;
  88.       let nameNode = document.createElement("h2");
  89.       nameNode.textContent = client.clientName;
  90.  
  91.       client.tabs.forEach(function({title, urlHistory, icon}) {
  92.         let pageUrl = urlHistory[0];
  93.  
  94.         // Skip tabs that are already open
  95.         if (engine.locallyOpenTabMatchesURL(pageUrl))
  96.           return;
  97.  
  98.         if (title == "")
  99.           title = pageUrl;
  100.  
  101.         let item = document.createElement("div");
  102.         item.addEventListener("click", function() {
  103.           item.setAttribute("selected", true);
  104.           window.location = pageUrl;
  105.         }, false)
  106.         item.setAttribute("class", "tab");
  107.  
  108.         let img = document.createElement("img");
  109.         img.setAttribute("class", "icon");
  110.         img.src = Weave.Utils.getIcon(icon, "chrome://weave/skin/tab.png");
  111.  
  112.         let tabDiv = document.createElement("div");
  113.         tabDiv.setAttribute("class", "info");
  114.         let titleNode = document.createElement("div");
  115.         titleNode.setAttribute("class", "title");
  116.         titleNode.textContent = title;
  117.         let urlNode = document.createElement("div");
  118.         urlNode.setAttribute("class", "url");
  119.         urlNode.textContent = pageUrl;
  120.         tabDiv.appendChild(titleNode);
  121.         tabDiv.appendChild(urlNode);
  122.  
  123.         item.appendChild(img);
  124.         item.appendChild(tabDiv);
  125.  
  126.         // Append the client name if we haven't yet
  127.         if (appendClient) {
  128.           appendClient = false;
  129.           holder.appendChild(nameNode);
  130.         }
  131.  
  132.         holder.appendChild(item);
  133.       });
  134.     }
  135.  
  136.     if (holder.childNodes.length == 0) {
  137.       // Assume we're pending, but we might already have tabs or have synced
  138.       let text = Weave.Str.sync.get("remote.pending.label");
  139.       if (haveTabs)
  140.         text = Weave.Str.sync.get("remote.opened.label");
  141.       else if (engine.lastSync != 0)
  142.         text = Weave.Str.sync.get("remote.missing.label");
  143.  
  144.       let item = document.createElement("h1");
  145.       item.textContent = text;
  146.       document.getElementsByTagName('body')[0].appendChild(item);
  147.     }
  148.   }
  149. };
  150.